package cc.linker.steplink.crm.merchanth5.service.coupon;

import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class H5IsLossGoodsService {

    /**
     * @Description: 满减后金额
     * @Param: [amount 价格, minUsePrice 满减金额，discountPrice 优惠金额]
     * @Return: java.util.Map
     * @Author: ch
     * @Date: 2020/8/29 13:20
     * @Version:
     **/
    public Long reduceAmount(Long amount,Integer minUsePrice,Integer discountPrice){
        //比例后的折扣金额（价格小于满减金额）
        Integer resDiscountPrice = discountPrice;
        if(amount>=minUsePrice && amount>=discountPrice){
            //价格大于满减金额，并且大于优惠金额
            amount = amount-discountPrice;
        }else if(amount>minUsePrice && amount<discountPrice){
            //价格大于满减金额，并且小于优惠金额
            amount = 0L;
        }else if(amount<minUsePrice){
            throw GlobalException.build("价格小于满减金额!");
            //价格小于满减金额
//            resDiscountPrice = new BigDecimal(discountPrice*amount.doubleValue()/minUsePrice).setScale(0, BigDecimal.ROUND_HALF_EVEN).intValue();
//            if(resDiscountPrice==0 && discountPrice>amount){
//                //进位后为0，优惠金额大于价格
//                amount = 0L;
//            }else if(resDiscountPrice==0 && discountPrice<amount){
//                //进位大于0，优惠金额小于价格
//                amount = amount - discountPrice;
//            }else if(resDiscountPrice>0 && resDiscountPrice>amount){
//                //进位大于0，优惠金额大于价格
//                amount = 0L;
//            }else if(resDiscountPrice>0 && resDiscountPrice<amount) {
//                amount = amount - resDiscountPrice;
//            }
        }
        return amount;
    }

    /**
     * @Description: 折扣后金额
     * @Param: [amount 价格, maxDiscountPrice 最大优惠金额，discountNum 折扣数]
     * @Return: java.util.Map
     * @Author: ch
     * @Date: 2020/8/29 13:20
     * @Version:
     **/
    public Long reduceAmountD(Long amount,Integer maxDiscountPrice,Integer discountNum){
        //折扣后金额
        int discountPrice = new BigDecimal(amount.doubleValue() - (amount.doubleValue()*discountNum/1000)).setScale(0, BigDecimal.ROUND_HALF_EVEN).intValue();
        discountPrice = Integer.valueOf(amount.toString()) - discountPrice;
        if((amount-discountPrice)>=maxDiscountPrice && maxDiscountPrice>0){
            //折扣金额大于最大优惠金额
            amount = amount-maxDiscountPrice;
        }else if((amount-discountPrice)<=maxDiscountPrice || maxDiscountPrice==0){
            //折扣金额小于最大优惠金额，且折扣金额大于0
            amount = (long)discountPrice;
        }
        return amount;
    }

    public static void main(String[] args) {
        Long amount = 1L;
        Integer maxDiscountPrice = 0;
        Integer discountNum = 100;
        //折扣后金额
        int discountPrice = new BigDecimal(amount.doubleValue()*discountNum/1000).setScale(0, BigDecimal.ROUND_HALF_EVEN).intValue();
        if((amount-discountPrice)>=maxDiscountPrice && maxDiscountPrice>0){
            //折扣金额大于最大优惠金额
            amount = amount-maxDiscountPrice;
        }else if((amount-discountPrice)<=maxDiscountPrice || maxDiscountPrice==0){
            //折扣金额小于最大优惠金额，且折扣金额大于0
            amount = (long)discountPrice;
        }
        System.out.println(amount);
    }
}
